package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.http.HttpAdapter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class NewsAdapter extends BaseListAdapter<LinkedTreeMap> {

    public NewsAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_news_item, parent, false);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                holder.title=(TextView) convertView.findViewById(R.id.title);
                holder.HITS=(TextView) convertView.findViewById(R.id.HITS);
                holder.image=(ImageView) convertView.findViewById(R.id.image);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            if (mDataList.get(position).containsKey("CREATE_TIME")){
                holder.time.setText(formatDate1(mDataList.get(position).get("CREATE_TIME")+""));
            }
            holder.title.setText(mDataList.get(position).get("TITLE")+"");

            Glide.with(mContext).load(HttpAdapter.HEAD_URL+mDataList.get(position).get("URL")).error(R.mipmap.zx_bg).into(holder.image);
            if (mDataList.get(position).containsKey("HITS")){
                holder.HITS.setText(decimalFormat.format(mDataList.get(position).get("HITS"))+"");
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView time, title, HITS;
        ImageView image;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    public static String formatDate1(String dateStr) {
        DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy KK:mm:ss aa", Locale.ENGLISH);
        Date date = null;
        try {
            date = formatFrom.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat formatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatTo.format(date)+"";
    }
}
